package com.zz.inventory.controller;

import java.util.List;
import java.io.IOException;
import java.util.stream.Collectors;

import jakarta.servlet.http.HttpServletResponse;
import org.example.dto.ExamsDTo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.zz.inventory.domain.Exams;
import com.zz.inventory.service.IExamsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 考试练习任务Controller
 * 
 * @author zz
 * @date 2025-07-01
 */
@RestController
@RequestMapping("/exams")
public class ExamsController extends BaseController
{
    @Autowired
    private IExamsService examsService;

    /**
     * 查询考试练习任务列表
     */
    @RequiresPermissions("Teacher:exams:list")
    @GetMapping("/list")
    public TableDataInfo list(Exams exams)
    {
        startPage();
        List<Exams> list = examsService.selectExamsList(exams);
        return getDataTable(list);
    }

    /**
     * 学生机，远程调用查询考试练习任务，将考试练习任务返回
     * @param
     * @return
     */

    @PostMapping("/studentlist")
    public ExamsDTo Studentlist(@RequestBody ExamsDTo examsDTo) {
        Exams exams = new Exams();
        exams.setSchoolId(examsDTo.getSchoolId());
        startPage();
        List<Exams> list = examsService.selectExamsList(exams);
        List<ExamsDTo> transferList = list.stream()
                .map(exam -> {
                    ExamsDTo dto = new ExamsDTo();
                    BeanUtils.copyProperties(exam, dto); // 字段对齐时直接复制
                    return dto;
                })
                .collect(Collectors.toList());
        ExamsDTo resultDTO = new ExamsDTo();
        resultDTO.setExamsList(transferList); // 假设 ExamsDTO 里有 List<ExamsTransferDTO> 字段
        return resultDTO;
    }


    /**
     * 导出考试练习任务列表
     */
    @RequiresPermissions("Teacher:exams:export")
    @Log(title = "考试练习任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Exams exams)
    {
        List<Exams> list = examsService.selectExamsList(exams);
        ExcelUtil<Exams> util = new ExcelUtil<Exams>(Exams.class);
        util.exportExcel(response, list, "考试练习任务数据");
    }

    /**
     * 获取考试练习任务详细信息
     */
    @RequiresPermissions("Teacher:exams:query")
    @GetMapping(value = "/{examId}")
    public AjaxResult getInfo(@PathVariable("examId") Long examId)
    {
        return success(examsService.selectExamsByExamId(examId));
    }

    /**
     * 新增考试练习任务
     */
    @RequiresPermissions("Teacher:exams:add")
    @Log(title = "考试练习任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Exams exams)
    {
        return toAjax(examsService.insertExams(exams));
    }

    @GetMapping("/labs/checkLabIdExists")
    public AjaxResult checkLabIdExists(@RequestParam Integer labId) {
        return toAjax(examsService.checkLabIdExists(labId));
    }

    @GetMapping("/schools/checkSchoolIdExists")
    public AjaxResult checkSchoolIdExists(@RequestParam Integer schoolsId) {
        return toAjax(examsService.checkSchoolIdExists(schoolsId));
    }

    /**
     * 修改考试练习任务
     */
    @RequiresPermissions("Teacher:exams:edit")
    @Log(title = "考试练习任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Exams exams)
    {
        return toAjax(examsService.updateExams(exams));
    }

    /**
     * 删除考试练习任务
     */
    @RequiresPermissions("Teacher:exams:remove")
    @Log(title = "考试练习任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{examIds}")
    public AjaxResult remove(@PathVariable Long[] examIds)
    {
        return toAjax(examsService.deleteExamsByExamIds(examIds));
    }
}
